<?php

namespace Chaosyeo\SolarEngine\Event;

use Chaosyeo\SolarEngine\Arrayable;
use Chaosyeo\SolarEngine\Properties as BaseProperties;

class Properties extends BaseProperties implements Arrayable
{
    const PLATFORM_OTHER     = 0;
    const PLATFORM_ANDROID   = 1;
    const PLATFORM_IOS       = 2;
    const PLATFORM_WINDOWS   = 3;
    const PLATFORM_MAC       = 4;
    const PLATFORM_WEB       = 5;
    const PLATFORM_DEVTOOLS  = 8;
    const PLATFORM_QUICKAPP  = 9;
    const PLATFORM_QUICKGAME = 10;
    const PLATFORM_HARMONYOS = 11;

    const MP_TYPE_WECHAT = 'wechat';
    const MP_TYPE_DOUYIN = 'douyin';
    const MP_TYPE_KWAI   = 'kwai';

    const APP_PLATFORM_WEB         = 'web';
    const APP_PLATFORM_ANDROID     = 'android';
    const APP_PLATFORM_IOS         = 'ios';
    const APP_PLATFORM_MINIPROGRAM = 'miniprogram';
    const APP_PLATFORM_MINIGAME    = 'minigame';

    // 设备类型枚举值
    const DEVICE_TYPE_OTHER         = 0;    // 其他设备
    const DEVICE_TYPE_ANDROID_PHONE = 1;    // 安卓手机
    const DEVICE_TYPE_ANDROID_PAD   = 2;    // 安卓平板
    const DEVICE_TYPE_IPHONE        = 3;    // iPhone
    const DEVICE_TYPE_IPAD          = 4;    // iPad
    const DEVICE_TYPE_MAC           = 5;    // Mac
    const DEVICE_TYPE_PC            = 6;    // PC
    const DEVICE_TYPE_DEVTOOLS      = 7;    // 开发者工具
    const DEVICE_TYPE_TV            = 8;    // 电视
    const DEVICE_TYPE_HARMONY_PHONE = 9;    // 鸿蒙手机
    const DEVICE_TYPE_HARMONY_PAD   = 10;   // 鸿蒙平板

    // 网络类型枚举值
    const NETWORK_TYPE_NONE       = 0;    // 无网络
    const NETWORK_TYPE_UNKNOWN    = 1;    // 未知网络
    const NETWORK_TYPE_2G         = 2;    // 2G网络
    const NETWORK_TYPE_3G         = 3;    // 3G网络
    const NETWORK_TYPE_4G         = 4;    // 4G网络
    const NETWORK_TYPE_5G         = 5;    // 5G网络
    const NETWORK_TYPE_6G         = 6;    // 6G或下一代网络
    const NETWORK_TYPE_WIFI       = 9;    // WIFI网络
    const NETWORK_TYPE_BLUETOOTH  = 10;   // 蓝牙网络（快应用）
    const NETWORK_TYPE_OTHERS     = 11;   // 其他网络（快应用）

    // SDK类型枚举值
    const LIB_TYPE_OTHER      = 0;    // 其他
    const LIB_TYPE_ANDROID    = 1;    // Android
    const LIB_TYPE_IOS        = 2;    // iOS
    const LIB_TYPE_JS         = 3;    // JavaScript
    const LIB_TYPE_MINIAPP    = 4;    // 小程序
    const LIB_TYPE_MINIGAME   = 5;    // 小游戏
    const LIB_TYPE_QUICKAPP   = 6;    // 快应用
    const LIB_TYPE_HARMONY    = 9;    // 鸿蒙
    const LIB_TYPE_QUICKGAME  = 10;   // 快游戏

    protected $caid;
    protected $openid;
    protected $anonymousOpenid;
    protected $country;
    protected $province;
    protected $city;
    protected $language;
    protected $timeZone;
    protected $manufacturer;
    protected $platform;
    protected $mpType;
    protected $appPlatform;
    protected $osVersion;
    protected $resolution;
    protected $screenHeight;
    protected $screenWidth;
    protected $density;
    protected $deviceModel;
    protected $deviceName;
    protected $deviceType;
    protected $networkType;
    protected $carrier;
    protected $appVersion;
    protected $appVersionCode;
    protected $packageName;
    protected $appName;
    protected $channel;
    protected $lib;
    protected $libVersion;
    protected $duration;
    protected $isFirstDay;
    protected $isFirstTime;
    protected $browser;
    protected $browserVersion;
    protected $pageTitle;
    protected $pageName;
    protected $referrerTitle;
    protected $referrerName;
    protected $adPersonalizationEnabled;
    protected $adUserDataEnabled;
    
    /**
     * @param int $platform 设备操作系统（必传）
     * @param array $params 可选参数数组，可包含：
     *   - mpType: string 小程序平台类型（小程序、小游戏必传）
     *   - appPlatform: string 应用系统平台（小程序、小游戏必传）
     *   - packageName: string 应用包名（Android、iOS、HarmonyOS必传）
     *   - isFirstDay: bool 首日事件标记（安装事件必传）
     *   - isFirstTime: bool 首次事件标记（安装事件必传）
     *   - caid: string iOS系统CAID
     *   - openid: string 小程序用户唯一ID
     *   - anonymousOpenid: string 抖音小程序匿名用户ID
     *   - country: string 用户所在国家
     *   - province: string 用户所在省份
     *   - city: string 用户所在城市
     *   - language: string 设备语言
     *   - timeZone: string 设备时区
     *   - manufacturer: string 设备制造商
     *   - osVersion: string 系统版本号
     *   - resolution: string 设备分辨率
     *   - screenHeight: int 屏幕高度
     *   - screenWidth: int 屏幕宽度
     *   - density: int 像素密度
     *   - deviceModel: string 设备型号
     *   - deviceName: string 设备名称MD5
     *   - deviceType: int 设备类型
     *   - networkType: int 网络状态
     *   - carrier: string 网络运营商
     *   - appVersion: string App版本
     *   - appVersionCode: string App Build版本号
     *   - appName: string 应用名称
     *   - channel: string App渠道包名称
     *   - lib: int SDK类型
     *   - libVersion: string SDK版本
     *   - duration: int 事件时长
     *   - browser: string 浏览器类型
     *   - browserVersion: string 浏览器版本
     *   - pageTitle: string 页面标题
     *   - pageName: string 页面地址
     *   - referrerTitle: string 跳转前页面标题
     *   - referrerName: string 跳转前页面地址
     *   - adPersonalizationEnabled: bool Google个性化广告许可
     *   - adUserDataEnabled: bool Google数据发送许可
     */
    public function __construct(int $platform, array $params = [], array $customProperties = [])
    {
        // 验证平台参数是否为有效的常量值
        if (!in_array($platform, [
            self::PLATFORM_OTHER,
            self::PLATFORM_ANDROID,
            self::PLATFORM_IOS,
            self::PLATFORM_WINDOWS,
            self::PLATFORM_MAC,
            self::PLATFORM_WEB,
            self::PLATFORM_DEVTOOLS,
            self::PLATFORM_QUICKAPP,
            self::PLATFORM_QUICKGAME,
            self::PLATFORM_HARMONYOS
        ])) {
            throw new \InvalidArgumentException('Invalid platform value');
        }
        $this->platform = $platform;

        // 验证小程序相关必传参数
        if (in_array($platform, [self::PLATFORM_QUICKAPP, self::PLATFORM_QUICKGAME])) {
            if (empty($params['mpType']) || empty($params['appPlatform'])) {
                throw new \InvalidArgumentException('mpType and appPlatform are required for miniprogram');
            }
        }

        // 验证移动应用必传参数
        if (in_array($platform, [self::PLATFORM_ANDROID, self::PLATFORM_IOS, self::PLATFORM_HARMONYOS])) {
            if (empty($params['packageName'])) {
                throw new \InvalidArgumentException('packageName is required for mobile apps');
            }
        }

        // 设置其他参数
        foreach ($params as $key => $value) {
            if (property_exists($this, $key)) {
                $this->$key = $value;
            }
        }

        parent::__construct($customProperties);
    }
}